home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 3_0 / STATUSWI / STATUSWI.C next >
Text File  |  1988-11-24  |  16KB  |  549 lines

  1. /* StatusWindow.c */
  2.  
  3. /*    StatusWindow XCMD for HyperCard
  4.     with LightSpeedC v. 3.01 - 11/20/88
  5.                                                     Copyright 1988 by
  6.     Puts up a modeless dialog that displays            Sam Thornton
  7.     Mac Model/CPU/FPU, number of KB free in            PO Box 123
  8.     the application heap, name, size, and free        Fairfield NE 68938
  9.     bytes of current stack, and the time.            Some portions Copyright
  10.     Buttons for basic HyperCard navigation,            by Symantec and Apple.
  11.     stack/heap compaction, date, and transfer
  12.     to other stacks/applications are provided.        Compuserve: 71750,667
  13.     See StatusWindow.docs for user interface        GEnie: S.THORNTON5
  14.                                                     
  15.     NOTE:  All 'StringPtr' types in                    May be copied and
  16.            HyperXCmd.h should                        distributed for personal
  17.            be dereferenced to type 'Ptr'            use without charge only,
  18.            for this to compile properly.            except for nominal
  19.                                                     distribution fees. For
  20.                                                     commercial use or
  21.                                                     distribution, contact
  22.                                                     author, above.     */
  23. #include "HyperXCmd.h"
  24. #include "SetUpA4.h"
  25. #include "MemoryMgr.h"
  26. #include "WindowMgr.h"
  27. #include "DialogMgr.h"
  28. #include "EventMgr.h"
  29. #include "MacTypes.h"
  30. #include "QuickDraw.h"
  31. #include "unix.h"
  32. #include "FileMgr.h"
  33. #include "DeviceMgr.h"
  34. #include "OSUtil.h"
  35. #include "strings.h"
  36. #include "FileMgr.h"
  37. #include "ResourceMgr.h"
  38.  
  39. pascal void SendCardMsg(Ptr msg);    /* OUR INTERNAL VERSIONS */
  40. pascal Handle EvalCmd(Ptr expr);    /* OF HC CALLBACKS       */
  41.  
  42. /* ===================================================================== */
  43. /* SOME CONSTANTS...                                                     */
  44. /* ===================================================================== */
  45. #define    MAIN_DIALOG 297        /* RESOURCE ID FOR OUR DIALOG                */
  46. #define _OPEN          10        /* OPEN button                               */
  47. #define _RECENT          9        /* RECENT button                             */
  48. #define _DATE_TIME      8        /* TIME button                               */
  49. #define _COMPACT      7        /* COMPACT button                            */
  50. #define _NEXT          6        /* NEXT button (uses HyperCard ICON)         */
  51. #define _HOME          5        /* HOME button (uses HyperCard ICON)         */
  52. #define _PREV          4        /* PREV button (uses HyperCard ICON)         */
  53. #define _MACHINE      3        /* ITEM ID FOR HARDWARE/MEM DISPLAY          */
  54. #define _TIME          2        /* ITEM ID FOR DATE/TIME DISPLAY             */
  55. #define _CURR_STACK      1        /* ITEM ID FOR CURRENT STACK INFO DISPLAY    */
  56. #define NULL          0L
  57. #define _QUIT          99        /* ARBITRARY VALUE FOR QUIT FLAG             */
  58. /* ===================================================================== */
  59.  
  60. typedef struct trade_mark {
  61.     char    mk[90];
  62.     } trade_mark;
  63.     
  64. trade_mark mrk = {" StatusWindow XCMD Copyright 1988 by Sam Thornton * PO Box 123 * Fairfield NE 68938 "};
  65.  
  66. /* ===================================================================== */
  67. /* SET ASIDE SOME GLOBALS FOR LAZIER PARAMETER MANAGEMENT                */
  68. /* ===================================================================== */
  69. static    char            machine[31];    /* short machine name            */
  70. static    DialogPtr        master_ptr;        /* Our dialog pointer            */
  71. static    WindowPtr        old_window;        /* HyperCard's Window            */
  72. static    long            cur_mem;        /* CHECK MEM FOR REDISPLAY       */
  73. static    int                dt_mode;        /* FLAG: DISPLAY DATE OR TIME    */
  74. static    long            the_ticks;        /* OUR GLOBAL COUNTDOWN TIMER    */
  75. static    Rect            myGlobalRect;    /* H/C SCREEN DISPLAY AREA       */
  76. static    XCmdBlockPtr    myParamPtr;        /* COPY OF HYPERCARD CALLBACK    */
  77. /* ===================================================================== */
  78. /*                             ENTRY POINT                               */
  79. /* ===================================================================== */
  80.  
  81. pascal void main (paramPtr)
  82. XCmdBlockPtr    paramPtr;
  83.   {
  84.     int                    itemHit, mem_size;
  85.     char                my_machine[31];
  86.       pascal Boolean        MyFilter();
  87.       GrafPtr                current_port;
  88.     SysEnvRec            environment;
  89.  
  90.       RememberA0();
  91.     SetUpA4();
  92.     
  93.     /* DO SOME CLEANUPS */
  94.     myParamPtr = paramPtr;
  95.     SendCardMsg("\phide msg"); /* Fixes la Bomba on selection of Msg Box */
  96.     SendCardMsg("\pset cursor to 4");
  97.     old_window = FrontWindow(); /* so we can kill HC window on XFERs */
  98.         
  99.     /* DESCRIBE THE AREA IN WHICH OUR DIALOG CAN BE MOVED                */
  100.     /* NOTE: can't seem to use 'screenBits.bounds' from HyperCard, so... */
  101.         GetPort(¤t_port);
  102.         myGlobalRect = current_port->portRect;
  103.         myGlobalRect.top += 20;
  104.         InsetRect (&myGlobalRect, 4, 4);
  105.     
  106.         SetDAFont(geneva);
  107.           master_ptr = GetNewDialog(MAIN_DIALOG, 0, -1L);
  108.         
  109.       /* CHECK TO MAKE SURE THERE WAS ENOUGH MEMORY */
  110.           if (master_ptr == NULL) { /* oops! -- we're gone */
  111.               SysBeep(1);
  112.               goto PANIC;
  113.             }
  114.     
  115.     /* GET THE MACHINE/CPU/FPU TYPE */
  116.  
  117.         SysEnvirons(99,&environment); /* NOTE: USE A HIGH VERS.NUMBER */
  118.  
  119.         switch(environment.machineType) {
  120.             case envMachUnknown:
  121.                 strcpy(my_machine,"NewMac");
  122.                 break;
  123.             case env512KE:
  124.                 strcpy(my_machine,"Mac512KE");
  125.                 break;
  126.             case envMacPlus:
  127.                 strcpy(my_machine,"MacPlus");
  128.                 break;
  129.             case envSE:
  130.                 strcpy(my_machine,"MacSE");
  131.                 break;
  132.             case envMacII:
  133.                 strcpy(my_machine,"MacII");
  134.                 break;
  135.             case envMac:
  136.                 strcpy(my_machine,"Mac64K");
  137.                 break;
  138.             case envXL:
  139.                 strcpy(my_machine,"MacXL");
  140.                 break;
  141.             default:
  142.                 strcpy(my_machine,"???Mac");
  143.                 break;
  144.             }
  145.         strcpy(machine,my_machine);
  146.         strcat(machine,": ");
  147.         switch(environment.processor) {
  148.             case envCPUUnknown:
  149.                 strcat(my_machine,"*NEW CPU");
  150.                 break;
  151.             case env68000:
  152.                 strcat(my_machine,"*68000");
  153.                 break;
  154.             case env68010:
  155.                 strcat(my_machine,"*68010");
  156.                 break;
  157.             case env68020:
  158.                 strcat(my_machine,"*68020");
  159.                 break;
  160.             default:
  161.                 strcat(my_machine,"*CPU???");
  162.                 break;
  163.             }
  164.         if (environment.hasFPU)
  165.             strcat(my_machine,"(68881)");
  166.         CtoPstr(my_machine);
  167.         SetWTitle(master_ptr, &my_machine);
  168.     
  169.     /* SET DATE/TIME FLAG FOR DISPLAY OF CURRENT TIME: 0=time, 1=date */
  170.         dt_mode = 0;
  171.           show_mem();
  172.           Get_Stack_Name();
  173.           ShowWindow(master_ptr);
  174.           InitCursor ();
  175.           
  176.       /* LOOP THRU MODALDIALOG UNTIL CLOSEBOX IS CLICKED */
  177.           do {
  178.  
  179.             ModalDialog(MyFilter, &itemHit);
  180.  
  181.             } while (itemHit != _QUIT);
  182.     
  183.     /* BYE....*/
  184.         DisposDialog(master_ptr);
  185. PANIC:
  186.         SetDAFont(systemFont);
  187.         compact_it();
  188.         RestoreA4();
  189.         return;
  190. }
  191.  
  192. /* ===================================================================== */
  193. /* MODALDIALOG FILTERPROC HANDLES OUR MEAGER REQUIREMENTS                */
  194. /* ===================================================================== */
  195.  
  196. pascal static Boolean MyFilter(dptr, theEvent, itemHit)
  197. DialogPtr dptr; EventRecord *theEvent; int *itemHit;
  198. {
  199.     int        theHit; /* LOCAL SPARE */
  200.     Handle    item_handle;
  201.     int        item_type;
  202.     Rect    item_rect;
  203.     Handle    answer;
  204.         
  205.     if (Ticks >= the_ticks) show_mem(); /* IF TIMER ELAPSED, UPDATE */
  206.         
  207.     if (dptr != master_ptr) return (false);
  208.     
  209.     /* IF NO MOUSEHIT, LET MODALDIALOG HANDLE THIS CYCLE */
  210.     if (theEvent->what != mouseDown) return (false);
  211.     
  212.     /* FIND OUT WHERE THE MOUSE WAS CLICKED AND HANDLE IT */
  213.     /* NOTE:  Since this is strictly a ModalDialog subroutine, we don't
  214.               have to worry about hits in other parts of the screen */
  215.               
  216.     switch(FindWindow(theEvent->where, &dptr)) {
  217.         
  218.         case inDrag:    /* DRAG DIALOG AROUND - NOTE: callback routine
  219.                            modified in XCMD.c to dereference msg param */
  220.         
  221.             DragWindow(dptr, theEvent->where, &myGlobalRect);
  222.             show_mem();
  223.             /* lazy update of HC screen */
  224.             SendCardMsg("\pgo this card");
  225.             
  226.             return(true); 
  227.             
  228.         case inGoAway: /* SET ITEMHIT TO OUR QUIT FLAG, IF APPROPRIATE */
  229.             if (TrackGoAway(dptr, theEvent->where)) *itemHit = _QUIT;
  230.             return(true);
  231.                 
  232.         case inContent: /* RESPOND TO HITS IN OUR DIALOG CONTENT REGION */
  233.             if (DialogSelect(theEvent, &dptr, &theHit)) {
  234.             
  235.                 switch(theHit) {
  236.         
  237.                 case _COMPACT:
  238.                     
  239.                     if (theEvent->modifiers & optionKey) {
  240.                         compact_it();
  241.                         SendCardMsg("\pset lockMessages to true");
  242.                         SendCardMsg("\pdoMenu \"Compact Stack\"");
  243.                         SendCardMsg("\pset cursor to 4");
  244.                         SendCardMsg("\pset lockMessages to false");
  245.                         comp_Mem();
  246.                         Get_Stack_Name();
  247.                         InitCursor();
  248.                         }
  249.                     else comp_Mem();
  250.                     return(true);
  251.                 
  252.                 case _DATE_TIME:
  253.                     dt_mode = 1;
  254.                     show_mem();
  255.                     return(true);
  256.                     
  257.                 case _OPEN:
  258.                     DoMyOpen();
  259.                     return(true);
  260.                 
  261.                 case _RECENT:
  262.                     set_card();
  263.                         
  264.                     if (theEvent->modifiers & optionKey) {
  265.                         SendCardMsg("\ppush card");
  266.                         SendCardMsg("\pdoMenu recent");
  267.                         }
  268.                     else {
  269.                     SendCardMsg("\pset cursor to 4");
  270.                     SendCardMsg("\ppop card");
  271.                     }
  272.                     SendCardMsg("\pset lockmessages to false");
  273.                     Get_Stack_Name();
  274.                     show_mem();
  275.                     InitCursor();
  276.                     return(true);
  277.                     
  278.                 case _PREV:
  279.                 case _NEXT:
  280.                 case _HOME:
  281.                     SetPort(master_ptr);
  282.                     GetDItem(master_ptr,theHit,&item_type,&item_handle,&item_rect);
  283.                     if (theHit == _HOME)
  284.                         InsetRect(&item_rect,1,5);
  285.                     else InsetRect(&item_rect,3,4);
  286.                     InvertRect(&item_rect);
  287.                     SendCardMsg("\pset lockmessages to true");
  288.                 
  289.                     if (theHit == _PREV) {
  290.                     SendCardMsg("\pvisual scroll right to gray");
  291.                     SendCardMsg("\pvisual scroll right");
  292.                     SendCardMsg("\pgo prev card");
  293.                     }
  294.                     else if (theHit == _NEXT) {
  295.                     SendCardMsg("\pvisual scroll left to gray");
  296.                     SendCardMsg("\pvisual scroll left");
  297.                     SendCardMsg("\pgo next card");
  298.                     }
  299.                     else if (theHit == _HOME) {
  300.                     SendCardMsg("\pset cursor to 4");
  301.                     push_card();
  302.                     SendCardMsg("\pgo home");
  303.                     Get_Stack_Name();
  304.                     InitCursor();
  305.                     }
  306.                     SetPort(dptr);
  307.                     InvertRect(&item_rect);
  308.                     SendCardMsg("\pset lockmessages to false");
  309.                     return(true);
  310.                 
  311.                 } /* END OF 'IN CONTENT' SWITCH */
  312.         
  313.         } /* END OF 'IF DIALOGSELECT' */
  314.         
  315.     } /* END OF FINDWINDOW SWITCH */
  316.     
  317.     return(false); /* nothing interesting happened--let's bore ModalDialog */
  318.     
  319. } /* END OF FUNCTION */
  320.  
  321. /* ===================================================================== */
  322. /* COMPACT THE APPLICATION HEAP AND JSR TO DISPLAY ROUTINE               */
  323. /* ===================================================================== */
  324.  
  325. comp_Mem()
  326. {
  327.     compact_it();
  328.     show_mem();
  329.     return;
  330. }
  331.  
  332. /* ===================================================================== */
  333. /* COMPACT THE APPLICATION HEAP                                          */
  334. /* ===================================================================== */
  335.  
  336. compact_it()
  337. {
  338.     Size    grow;
  339.     
  340.     MaxMem(&grow);
  341.     MaxApplZone();
  342.     return;
  343. }
  344.  
  345. /* ===================================================================== */
  346. /* FUNCTION TO DISPLAY CURRENT MEMSIZE & TIME (OR DATE) IN OUR DIALOG    */
  347. /* ===================================================================== */
  348.  
  349. show_mem()
  350. {
  351.     char        time_string[11];
  352.     char        mem_string[11];
  353.     Handle        item_handle;
  354.     int            item_type;
  355.     Rect        item_rect;            
  356.     char        temp_string[256];
  357.     GrafPtr        old_port;
  358.     
  359.     GetPort(&old_port);
  360.     SetPort(master_ptr);
  361.     PenNormal();
  362.     TextSize(9); /* 9-POINT */
  363.     
  364.     /* GET AVAILABLE MEMORY */
  365.     if (TheZone->zcbFree != cur_mem) {
  366.         cur_mem = TheZone->zcbFree;
  367.         strcpy(temp_string, machine);
  368.         stci_d(mem_string, (int)(cur_mem/1024), 9);
  369.         strcat(temp_string, mem_string);
  370.         strcat(temp_string,"K free\0");
  371.         CtoPstr(temp_string);
  372.         GetDItem(master_ptr,_MACHINE,&item_type,&item_handle,&item_rect);
  373.         SetIText(item_handle, temp_string);
  374.         }
  375.     
  376.     /* WE EITHER WANT THE CURRENT TIME...*/
  377.     if (dt_mode == 0) {
  378.         /* '1' = HH:MM:SS format */
  379.         IUTimeString(Time, 1, &time_string);
  380.         strcpy(temp_string,"Time: \0");
  381.         }
  382.     /* ...OR WE WANT THE CURRENT DATE */
  383.     else if (dt_mode == 1) {
  384.         /* '0' = MM/DD/YY format */
  385.         IUDateString(Time, 0, &time_string);
  386.             strcpy(temp_string,"Date: \0");
  387.         dt_mode = 0;
  388.         }
  389.     PtoCstr(time_string);
  390.     strcat(temp_string,time_string);
  391.     CtoPstr(temp_string);
  392.     GetDItem(master_ptr,_TIME,&item_type,&item_handle,&item_rect);
  393.     SetIText(item_handle, temp_string);
  394.     the_ticks = Ticks + 55; /* RESET OUR TIMER */
  395.     SetPort(old_port);
  396.     return;
  397. }
  398.  
  399. /* ===================================================================== */
  400. /*                      GET THE NAME OF THE CURRENT STACK                */
  401. /* ===================================================================== */
  402.  
  403. Get_Stack_Name()
  404. {
  405.     Handle    answer;
  406.     char    temp_string[128];
  407.     Handle    item_handle;
  408.     int        item_type;
  409.     Rect    item_rect;
  410.     
  411.     answer = EvalCmd( "\pthe short name of this stack");
  412.     strcpy(temp_string, "\"");
  413.     strcat(temp_string, *answer);
  414.     strcat(temp_string, "\" : \0");
  415.     DisposHandle(answer);
  416.     
  417.     answer = EvalCmd( "\pround(the size of this stack/1024)");
  418.     strcat(temp_string, *answer);
  419.     strcat(temp_string, "K : \0");
  420.     DisposHandle(answer);
  421.     
  422.     answer = EvalCmd( "\pround(the freesize of this stack/1024)");
  423.     strcat(temp_string, *answer);
  424.     strcat(temp_string, "K free");
  425.     DisposHandle(answer);
  426.     
  427.     CtoPstr(temp_string);
  428.     GetDItem(master_ptr,_CURR_STACK,&item_type,&item_handle,&item_rect);
  429.     SetIText(item_handle, temp_string);
  430.     return;
  431. }
  432.  
  433. /* ===================================================================== */
  434. /*                      OPEN ANOTHER APPLICATION OR STACK                */
  435. /* ===================================================================== */
  436.  
  437. DoMyOpen()
  438. {
  439.  
  440.     SFTypeList    myTypes;
  441.     Point         SFGwhere;
  442.     SFReply        reply;
  443.     char        fn[64];
  444.     char        msg[256];
  445.     
  446.     myTypes[0] = 'APPL';
  447.     myTypes[1] = 'STAK';
  448.     SFGwhere.v = 90; /* NOTE: this is set for standard Mac/Mac+/SE size */
  449.     SFGwhere.h = 82;
  450.     
  451.     SetDAFont(systemFont);
  452.     SFGetFile( SFGwhere, "\p", 0L, 2, &myTypes, 0L, &reply );
  453.     SendCardMsg("\pgo this card");
  454.     
  455.     if (reply.good) {
  456.         SendCardMsg("\pset cursor to 4");
  457.         pStrCopy( reply.fName, fn );
  458.         PtoCstr(fn);
  459.         if (reply.fType == 'STAK') {
  460.             strcpy(msg, "go to stack \"");
  461.             strcat(msg, fn);
  462.             strcat(msg, "\"");
  463.             CtoPstr(msg);
  464.             push_card();
  465.             SendCardMsg(msg);
  466.             SendCardMsg("\pset lockmessages to false");
  467.             Get_Stack_Name();
  468.             }
  469.         else {
  470.             if (strcmp(fn, "HyperCard\0") != 0) {
  471.                 
  472.                 strcpy(msg, "open \"");
  473.                 strcat(msg, fn);
  474.                 strcat(msg, "\"");
  475.                 CtoPstr(msg);
  476.                 DisposDialog(master_ptr);
  477.                 HideWindow(old_window);
  478.                 SendCardMsg("\pgo this card");
  479.                 SendCardMsg(msg);
  480.                 return;
  481.             }
  482.             else {
  483.                 SysBeep(1); /* can't open HyperCard from HyperCard */
  484.                 SysBeep(1);
  485.                 }
  486.         }
  487.     }
  488. SetDAFont(geneva);
  489. InitCursor();
  490. return;
  491. }
  492.  
  493. /* ===================================================================== */
  494. /*                           HYPERCARD CALLBACK SUB                      */
  495. /* ===================================================================== */
  496.  
  497. push_card()
  498. {
  499.     SendCardMsg("\ppush card");
  500.     set_card();
  501.     return;
  502. }
  503.  
  504. /* ===================================================================== */
  505. /*                           HYPERCARD CALLBACK SUB                      */
  506. /* ===================================================================== */
  507.  
  508. set_card()
  509. {
  510.     SendCardMsg("\pset lockmessages to true");
  511.     SendCardMsg("\pvisual dissolve to gray");
  512.     SendCardMsg("\pvisual venetian blinds");
  513.     return;
  514. }
  515.  
  516. /* ===================================================================== */
  517. /*                     ROUTINE TO COPY A PASCAL STRING                   */
  518. /* ===================================================================== */
  519.  
  520. pStrCopy( p1, p2 )
  521. register char *p1, *p2;
  522. /* copies a pascal string from p1 to p2 */
  523. {
  524.     register int len;
  525.     
  526.     len = *p2++ = *p1++;
  527.     while (--len>=0) *p2++=*p1++;
  528. }
  529.  
  530. /* ===================================================================== */
  531. /*                       MOD. HYPERCARD CALLBACK SUBS                    */
  532. /* ===================================================================== */
  533.  
  534. pascal void SendCardMsg(msg)
  535. Ptr msg;
  536. {
  537.     myParamPtr->inArgs[0] = (long)msg;
  538.     myParamPtr->request = xreqSendCardMessage;
  539.     (*myParamPtr->entryPoint)();
  540. }
  541.  
  542. pascal Handle EvalCmd(expr)
  543. Ptr    expr;
  544. {
  545.     myParamPtr->inArgs[0] = (long)expr;
  546.     myParamPtr->request = xreqEvalExpr;
  547.     (*myParamPtr->entryPoint)();
  548.     return (Handle)myParamPtr->outArgs[0];
  549. }